/**
 * @param {EmbeddedView} shell
 * @constructor
 */
function EmbeddedNavigationMediator(shell)
{
	"use strict";

	/** @type {EmbeddedNavigationMediator} */
	var _nav = this;
	var $next = $('#next');
	var $previous = $('#previous');
	/** @const */
	var _enabled = "enabled";

	function init()
	{
		$('div.edge')
			.hover(
				function() { $(this).find('.nav').fadeTo(200, 1); },
				function() { $(this).find('.nav').fadeTo(200, 0.3); }
			)
			.find('.nav').fadeTo(1000, 0.3);

		$next.click(tryNext);
		$previous.click(tryPrevious);

		$(window).keydown(function(e)
		{
			switch (e.which)
			{
				case 32:									// space bar
				case 39:				                    // forward arrow
					tryNext();
					break;
				case 37:                			        // back arrow
					tryPrevious();
					break;
				case 27:									// escape
					shell.slideshow.stop();
					break;
			}
		});
	}

	/**
	 * @param {Event} [e]
	 */
	function tryNext(e)
	{
		go($next, _nav.next, e);
	}

	/**
	 * @param {Event} [e]
	 */
	function tryPrevious(e)
	{
		go($previous, _nav.previous, e);
	}

	/**
	 * Show next or previous photo
	 * @param {jQuery} $el DOM element
	 * @param {Function} showPhoto Either nextPhoto() or previousPhoto()
	 * @param {Event} [e]
	 */
	function go($el, showPhoto, e)
	{
		if (e) { e.preventDefault(); }

		if ($el.data(_enabled))
		{
			shell.slideshow.stop();
			showPhoto();
		}
	}

	/**
	 * @param {Photo} photo
	 * @return {EmbeddedNavigationMediator}
	 */
	this.update = function(photo)
	{
		enableIf($previous, photo.previous);
		enableIf($next, photo.next);

		return _nav;
	};

	/**
	 * Update data element indicating if navigation is enabled
	 * @param {jQuery} $el
	 * @param {Photo} photo
	 */
	function enableIf($el, photo) { $el.data(_enabled, (photo != null)); }

	/**
	 * Shadow to implement
	 */
	this.next = function() { };

	/**
	 * Shadow to implement
	 */
	this.previous = function() { };

	init();
}